package model

import (
	"fmt"
	"jingzhoulianxin/db"
	"time"
)

type accountFlow struct {
	Id             int    `gorm:"primary_key;column:id" json:"id"`
	EmployeeId     int    `gorm:"column:employee_id" json:"employeeId"`
	Money          int    `gorm:"column:money" json:"money"`
	Cate           string `gorm:"column:table_name" json:"tableName"`
	Cash           int    `gorm:"column:cash" json:"cash"`
	Subsidy        int    `gorm:"column:subsidy" json:"subsidy"`
	CashBalance    int    `gorm:"column:cash_balance" json:"cashBalance"`
	SubsidyBalance int    `gorm:"column:subsidy_balance" json:"subsidyBalance"`
	CreateTime     time.Time `gorm:"column:create_time" json:"createTime"`
	Description    string `gorm:"column:description" json:"description"`
}

type accountFlowData struct {
	Id             int    `gorm:"primary_key;column:id" json:"id"`
	EmployeeId     int    `gorm:"column:employee_id" json:"employeeId"`
	Money          int    `gorm:"column:money" json:"money"`
	Cate           string `gorm:"column:table_name" json:"tableName"`
	Cash           int    `gorm:"column:cash" json:"cash"`
	Subsidy        int    `gorm:"column:subsidy" json:"subsidy"`
	CashBalance    int    `gorm:"column:cash_balance" json:"cashBalance"`
	SubsidyBalance int    `gorm:"column:subsidy_balance" json:"subsidyBalance"`
	CreateTime     time.Time `gorm:"column:create_time" json:"createTime"`
	Mobile         string `gorm:"column:mobile" json:"mobile"`
	DeviceSerial   string `gorm:"column:device_serial" json:"deviceSerial"`
	Remark         string `gorm:"column:remark" json:"remark"`
	Description    string `gorm:"column:description" json:"description"`
}


func (this *accountFlow) TableName() string {
	return "account_flow"
}

func AccountFlowList(maxId int) (err error, lists []accountFlowData) {
	dbs :=db.LocalDb()

	tx := dbs.Table("account_flow").
		Select("account_flow.id,account_flow.employee_id,account_flow.money,table_name,account_flow.cash,account_flow.subsidy,account_flow.cash_balance,account_flow.subsidy_balance,account_flow.create_time,account_flow.description,account.username as mobile,consume.device_serial").
		Joins("join account on account_flow.employee_id=account.id").
		Joins("left join consume on account_flow.table_id=consume.id and account_flow.type = 5").
		Where(fmt.Sprintf("account_flow.id > %d", maxId))

	err = tx.Find(&lists).Error
	defer dbs.Close()
	return
}

